package com.jiaai.cloud.api.pay;


import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jiaai.cloud.api.pay.entity.OrderInfo;
import com.jiaai.cloud.api.pay.utils.AlipayUtils;
import com.jiaai.cloud.base.exception.ServiceException;
import com.jiaai.cloud.base.util.IdWorkUtil;
import com.jiaai.cloud.base.util.RestResultGenerator;
import com.jiaai.cloud.order.entity.TPayOrder;
import com.jiaai.cloud.order.service.ITPayOrderService;
import com.jiaai.cloud.system.service.ITPayManageService;
import com.jiaai.cloud.utils.HttpHeaderUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * TODO
 * </p>
 *
 * @author 佳爱网络科技
 * @version V1.0
 * @title PayAPI
 * @since 2017/8/7
 */
@RequestMapping("/api/v1")
@RestController
public class PayAPI {
    private  final  static  String  APP_Id="2017041806791530";
    private final  static  String  RSA2_PRIVATE="MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCb+/kwu1ihUZe4tLWEtI0UODEYzaVXLI+pZbRyjTKAjjXm4u/sZg/dh3g9b7DWp7uW2ABoV7dKA6+EDqHG0hJYHgQHj/wA8UYHgEJbyah+CX81Y1gtoX3v8iUhS7uwsQ+vnwkdBoqrap7p7TybZCVSYIK7HHT4aunrc7F9ZbDYSrm2FWje9rpwTB3bU2xPH2+gf1irUsRJyW+oIm891u06v6NqrxISeQnTme9Mthe9a61hDue4thhorj96+sN8GImVs30A3yL/6gdDc4xSKJj7h5WR5QfdD1yE/XI29NAqfrftC340yr9CpsS0fDxYGQe68yw0I3lN/FmHDkc2rXDPAgMBAAECggEAS0jnMbUKhvnJf7WAcqdt/BdO4wjGR3fTctAKssshV9jLfT+EyxVLjQFIcGaK9F+Kc7iXIcVMSqXSvasOO9bCUdNS4I+G8rGAujgWTxT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isOjb7V0p1pRWwb6mZ81IIJ2XytoWDrGFuZOoZgwEiTZr8=";
    @Autowired
    private ITPayOrderService orderService;
    @Autowired
    private IdWorkUtil idWorkUtil;
    @Autowired
    private ITPayManageService payManageService;
    /**
     * 支付宝支付
     * @param orderInfo
     * @return
     */
    @PostMapping("/alipay/pay")
    public Object  alipayPay(@RequestBody OrderInfo orderInfo) throws ServiceException, AlipayApiException {
            Map<String,Object> dataMap=new HashMap<>();
            try {
                orderInfo.setOrderNo(idWorkUtil.generateId("payOrderNo",4));
            }catch (Exception e){
                throw  new ServiceException("生成订单号失败!",e.getMessage());
            }
            try {
                Map<String, String> params= AlipayUtils.buildOrderParamMap(APP_Id,orderInfo);
                String orderParam = AlipayUtils.buildOrderParam(params);
                String sign = AlipayUtils.getSign(params, RSA2_PRIVATE);
                dataMap.put("orderIno",orderParam+"&"+sign);
            }catch (Exception e){
                 throw  new ServiceException("签名失败!",e.getMessage());
            }
        String content="{\"timeout_express\":\"30m\",\"product_code\":\"QUICK_MSECURITY_PAY\",\"total_amount\":\""+20.00+"\",\"subject\":\""+orderInfo.getSubject()+"\",,\"out_trade_no\":\"" + orderInfo.getOrderNo() +  "\"}";
        System.out.println(AlipaySignature.rsaSign(content,RSA2_PRIVATE,"utf-8","RSA2"));
            try {
                TPayOrder order=new TPayOrder();
                order.setBody(orderInfo.getBody());
                order.setGold(orderInfo.getGold());
                order.setUserId(HttpHeaderUtils.getUserId());
                order.setMoney(orderInfo.getMoney());
                order.setPaType(1);
                order.setStatus(0);
                order.setOrderNo(orderInfo.getOrderNo());
                order.setTime(new Date());
                order.setSubject(orderInfo.getSubject());
                orderService.insert(order);
            }catch (Exception e){
                throw  new ServiceException("生成订单失败",e.getMessage());
            }
          return RestResultGenerator.genSuccessResult(dataMap);
    }

    @GetMapping("/pay/list")
    public Object getPayList()throws ServiceException{
        try {
            return  RestResultGenerator.genSuccessResult(payManageService.selectList(new EntityWrapper().orderBy("money",true)));
        }catch (Exception e){
            throw new ServiceException("获取充值列表失败!",e.getMessage());
        }
    }
}
